package com.itheima.service.impl;

import com.itheima.dao.AccountDao;
import com.itheima.service.AccountService;
import com.itheima.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;


@Service

public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    //1.注入日志业务对象
    @Autowired
    private LogService logService;

    /**
     * 转账业务
     * @param out   转账人的名字
     * @param in    收账人的名字
     * @param money  转账金额
     */
    // 开启事务
//    @Transactional
    // 只读事务
//    @Transactional(readOnly = true)
    // 回滚的异常演示   指定异常做回滚 （spring ）
//    @Transactional(rollbackFor = {IOException.class,ClassNotFoundException.class})
    //指定异常不做回滚
//    @Transactional(noRollbackFor = {ArithmeticException.class})
//    修改事务的隔离级别（默认 可重复读）
//    @Transactional(isolation = Isolation.READ_COMMITTED)
//    超时事务
    @Transactional(timeout = 2)

//    @Transactional
    public void transfer(String out,String in ,Double money) throws IOException, ClassNotFoundException {
        accountDao.outMoney(out,money);
//        int i = 1/0;//ava.lang.ArithmeticException:
//        if(1==1){
//            throw new ClassNotFoundException("我出问题了，你会回滚吗");
//        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        accountDao.inMoney(in,money);


//        try {
//            accountDao.outMoney(out,money);
//            int i = 1/0;
//            accountDao.inMoney(in,money);
//        } finally {
//            logService.log(out,in,money);
//        }
    }

}
